#include <SDL/SDL.h>  /* All SDL App's need this */
#include <stdio.h>

void display_bmp(const char*filename,SDL_Surface *screen);

int ShowBmpBySDL(const char*filename) {
    SDL_Surface *screen=NULL;
    printf("Initializing SDL.\n");
    
    /* Initialize defaults, Video and Audio */
    if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) { 
        printf("Could not initialize SDL: %s.\n", SDL_GetError());
        exit(-1);
    }

    printf("SDL initialized.\n");


	screen = SDL_SetVideoMode(640, 480, 24, SDL_SWSURFACE);
	display_bmp(filename,screen);

    printf("Quiting SDL.\n");
    
    /* Shutdown all subsystems */
    SDL_Quit();
    
    printf("Quiting....\n");

    exit(0);
}

void display_bmp(const char *filename,SDL_Surface *screen){
	 SDL_Surface*image=SDL_LoadBMP(filename);
	 if(image==NULL){
        fprintf(stderr, "Couldn't load %s: %s\n", filename, SDL_GetError());
        return;
	}
//set color
	if (image->format->palette && screen->format->palette) {
        SDL_SetColors(screen,image->format->palette->colors,0,image->format->palette->ncolors);
    }
    /* Blit onto the screen surface */
    if(SDL_BlitSurface(image, NULL, screen, NULL)<0){
       fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());
	}
    SDL_UpdateRect(screen, 0, 0, image->w, image->h);	
    /* Free the allocated BMP surface */
    SDL_FreeSurface(image);
	SDL_Delay(2000);
}